package com.example.spring_hello.controller;

import com.example.spring_hello.dao.HelloDao;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "ServletHello", value = "/ServletHello")
public class ServletHello extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring.xml");
        ServletContext servletContext = request.getServletContext();
        //ApplicationContext applicationContext = (ApplicationContext)servletContext.getAttribute("context");
        //ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        HelloDao hello = webApplicationContext.getBean("hello", HelloDao.class);
        hello.hello();
    }
}
